package org.alex.samples.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.metamodel.Bindable;
import javax.validation.Valid;

import org.alex.samples.model.ExamTest;
import org.alex.samples.model.ExamTestType;
import org.alex.samples.model.ExamTestVO;
import org.alex.samples.model.PagerModel;
import org.alex.samples.model.Subject;
import org.alex.samples.model.SubjectVO;
import org.alex.samples.model.User;
import org.alex.samples.service.ISubjectService;
import org.alex.samples.service.ManageTestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping({ "/test" })
public class ManageTestController {

	@Autowired
	ManageTestService manageTestService;
	
	@Autowired
	private ISubjectService subjectService;

	@RequestMapping(value = {"/tests"})
	public String findAllTests(Model model) throws Exception {
		PagerModel<ExamTestVO> testVOList = manageTestService.findAllByPage();
		model.addAttribute("testVOList", testVOList);
		
		List<SubjectVO> subjects = null;
		subjects = subjectService.findAllSubject();
		model.addAttribute("subjectList", subjects);
		return "managetest/testList";
	}

	@ResponseBody
	@RequestMapping(value = "/getTestById")
	public ExamTestVO getTestByID(Integer testId) throws Exception {	
		ExamTestVO reultVO = manageTestService.findByIDTestAndSub(testId);
		reultVO.setExamTestType(null);
		return reultVO;
	}

	@ResponseBody
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public Map<String,Object> addTest(@Validated ExamTest examTest, BindingResult bindingResult) throws Exception {
		Map<String,Object> messageMap = new HashMap<String,Object>();
		if (bindingResult.hasErrors()) {
			List<ObjectError> errorList = bindingResult.getAllErrors();
			for (ObjectError error : errorList) {
				FieldError fe = (FieldError) error;
				messageMap.put(fe.getField(), fe.getDefaultMessage());
			}
			return messageMap;
		}
		messageMap.put("success", true);
		manageTestService.createTest(examTest);
		return messageMap;
	}

	@ResponseBody
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	public Map<String,Object> editTest(@Validated ExamTest examTest, BindingResult bindingResult) throws Exception {
		Map<String,Object> messageMap = new HashMap<String,Object>();
		if (bindingResult.hasErrors()) {
			List<ObjectError> errorList = bindingResult.getAllErrors();
			for (ObjectError error : errorList) {
				FieldError fe = (FieldError) error;
				messageMap.put(fe.getField(), fe.getDefaultMessage());
			}
			return messageMap;
		}
		messageMap.put("success", true);
		manageTestService.editTest(examTest);
		return messageMap;
	}

	@ResponseBody
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public Map<String,Object> deleteTest(Integer testId) {
		Map<String,Object> messageMap = new HashMap<String,Object>();
		try {
			manageTestService.deleteTest(testId);
			messageMap.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			messageMap.put("success", false);
		}
		return messageMap;
	}

	@ResponseBody
	@RequestMapping(value = "/getTypes", method = RequestMethod.POST)
	public List<String> getTestTypes() throws Exception {
		List<ExamTestType> list = manageTestService.findAllTestType();
		List<String> map = new ArrayList<String>();
		for (int i = 0; i < list.size(); i++) {
			map.add(list.get(i).getId().toString());
			map.add(list.get(i).getTestType());
			map.add(list.get(i).getTestTypeScore().toString());
		}
		return map;
	}

	@ResponseBody
	@RequestMapping(value = "/getType", method = RequestMethod.POST)
	public Map<String, String> getTestTypeById(Integer typeId) throws Exception {
		ExamTestType testType = manageTestService.getTestTypeById(typeId);
		Map<String, String> map = new HashMap<String, String>();
		map.put("testTypeId", testType.getId().toString());
		map.put("testTypeName", testType.getTestType());
		map.put("testTypeScore", testType.getTestTypeScore().toString());
		return map;
	}
	
	@RequestMapping(value ={ "/subTest/{id}","/subTest/tests"})
	public String findTestBySubId(Model model,@PathVariable Integer id) throws Exception {
		PagerModel<ExamTestVO> testVOList = manageTestService.findTestBySubId(id);
		model.addAttribute("testVOList", testVOList);
		return "managetest/testList";
	}

}
